export default class Basic {
    constructor () {
        this.id = null;
        this.version = '1.4.15';
        this.key = '0781b981fd0a7eb07de1eba8dc6c9b3b';
        this.AMap = null;
        this.map = null;
        this.IpOrGps = false;
    }

    Load (ele, opts = {}) {
        let vm = this;
        return new Promise(resolve => {
            vm.AMap = window.AMap;
            vm.map = new vm.AMap.Map(ele || 'amap-container__global', {
                resizeEnable: true,
                zoom: 10,  //设置地图显示的缩放级别
                viewMode: '2D',  //设置地图模式
                lang: 'zh_cn',  //设置地图语言类型
                ...opts
            });
            console.log(vm.map);
            vm.map.on('complete', function () {
                // 地图图块加载完成后触发
                console.log('地图加载完成');
            });
            resolve(vm.map);
        })
    }

    loadMapApi (ele, opts = {}) {
        const vm = this;
        const version = this.version;
        const key = this.key;
        if (ele && Object.toString.call(ele) === '[object object]') {
            opts = {...ele};
            ele = opts.ele;
            delete opts.ele
        } else if (typeof ele !== "string") {
            ele = ''
        }
        if (vm.map) return Promise.resolve(vm.map);

        if (window.AMap) return new Promise(resolve => vm.Load(ele, opts));

        return new Promise(resolve => {
            let document = window.document;
            window.onAMapLoad = function () {
                vm.Load(ele, opts);
                resolve();
            };
            const url = `//webapi.amap.com/maps?v=${version}&&key=${key}&callback=onAMapLoad`;
            const jsapi = document.createElement('script');
            jsapi.charset = 'utf-8';
            jsapi.src = url;
            jsapi.id = `amap_id_${Date.now()}`;
            this.id = jsapi.id;
            document.head.appendChild(jsapi);
        })
    }

    destroy () {
        this.map && this.map.destroy()
    }

    plugin (str, opts = {}, bol = true) {
        let vm = this;
        let AMap = vm.AMap;
        if (typeof str !== "string" || Array.isArray(str)) return Promise.reject();
        if (!AMap) return Promise.reject();
        return new Promise(resolve => {
            AMap.plugin(str, function () {//异步加载插件
                if (!vm.map || !bol) resolve();
                let P = [];
                if (typeof str === "string") {
                    let plugin = vm.addControl(str);
                    resolve(plugin);
                } else {
                    str.map(v => vm.addControl(v));
                    resolve(str);
                }
            });
        })
    }

    addControl (str, resolve) {
        let List = ['AMap.ToolBar', 'AMap.Geolocation'];
        let AMap = this.AMap;
        let map = this.map;
        if (List.indexOf(str) > -1 && map && AMap) {
            let key = str.split('.');
            const toolbar = new AMap[key[key.length - 1]]();
            map.addControl(toolbar);
            return toolbar;
        }
    }
}
